package com.da.lon.wang.xlg.imgupload;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.activity.BaseActivity;
import com.da.lon.wang.xlg.activity.HomeActivity;
import com.da.lon.wang.xlg.bean.FirstEvent;
import com.da.lon.wang.xlg.bean.ModifyPhotoBean;
import com.da.lon.wang.xlg.bean.MusicSceneBean;
import com.da.lon.wang.xlg.bean.ResultBean;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListenerReplay;
import com.da.lon.wang.xlg.util.MyBitmapFactory;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.WebViewUtil;
import com.da.lon.wang.xlg.util.XLGDataUtils;

import net.tsz.afinal.http.AjaxParams;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class ChooseImageAvtivity extends BaseActivity implements
        OnClickListener {

    // 从Intent获取图片路径的KEY
    public static final String KEY_PHOTO_PATH = "photo_path";
    public static final String TYPE_REPLAY = "replay";
    public static final String TYPE_TOPIC = "topic";
    public static final String TYPE_MUSIC = "music";
    public static final String TYPE_MODIFY_PHOTO = "modify_photo";
    public static final String TYPE_COMMENT_ORDER = "comment_order";
    public static final String TYPE_SELL_MESSAGE = "sell_order";
    public static final String TYPE_SUGGEST_MESSAGE = "suggest_message";
    public static final String TYPE_CUSTOMER_UPLOAD = "customer_upload";
    public static final String TYPE_ORDER_PICTURE = "order_picture";
    public static String selectUri = "";
    private GridView gridView;
    private EditText editText;
    private Button sure;
    private String type;

    private boolean mOnlyChoose;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xlg_image_choose);
        mOnlyChoose = getIntent().getBooleanExtra("only_choose", false);
        initView();
        WebViewUtil.isHaveChoose = true;
    }

    private void initView() {
        ((ImageButton) findViewById(R.id.image)).setVisibility(View.GONE);
        ((RelativeLayout) findViewById(R.id.top_rl)).setBackgroundColor(Color
                .parseColor("#f0f0f0"));
        TextView title = (TextView) findViewById(R.id.title);
        title.setText("选择照片");
        title.setTextColor(Color.parseColor("#000000"));
        ImageButton back = (ImageButton) findViewById(R.id.back);
        back.setImageResource(R.drawable.xlg_back_grey);
        back.setOnClickListener(this);
        sure = (Button) findViewById(R.id.sure);
        sure.setOnClickListener(this);
        gridView = (GridView) findViewById(R.id.grid);
        editText = (EditText) findViewById(R.id.edit);
        type = HomeActivity.uploadType;
        // JavaScriptExit serializable =
        // (JavaScriptExit)getIntent().getExtras().getSerializable("javaScriptExit");
        if (type.equals(TYPE_REPLAY) && WebViewUtil.replayInfo != null) {
            editText.setText(new String(Base64.decode(
                    WebViewUtil.replayInfo.getComment_name(), Base64.DEFAULT)));
        }

        if (type.equals(TYPE_TOPIC)) {
            editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(
                    250)});
            editText.setVisibility(View.VISIBLE);
        } else if (type.equals(TYPE_REPLAY)) {
            editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(
                    250)});
            editText.setVisibility(View.VISIBLE);
        } else if (type.equals(TYPE_MUSIC)) {
            LayoutParams layoutParams = new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(XLGDataUtils.dipToPixel(5),
                    XLGDataUtils.dipToPixel(5), XLGDataUtils.dipToPixel(5),
                    XLGDataUtils.dipToPixel(5));
            editText.setLines(1);
            editText.setHint("请输入标题");
            editText.setLayoutParams(layoutParams);
            editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(
                    10)});
            editText.setVisibility(View.VISIBLE);
        } else if (type.equals(TYPE_MODIFY_PHOTO)) {
            editText.setVisibility(View.GONE);
            modifyPhoto();
            finish();
        } else if (type.equals(TYPE_COMMENT_ORDER)) {
            editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(
                    10)});
            editText.setVisibility(View.GONE);
        } else if (type.equals(TYPE_SELL_MESSAGE)) {
            editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(
                    10)});
            editText.setVisibility(View.GONE);
        } else if (type.equals(TYPE_SUGGEST_MESSAGE)) {
            editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(
                    10)});
            editText.setVisibility(View.GONE);
        } else if (type.equals(TYPE_CUSTOMER_UPLOAD)) {
            editText.setVisibility(View.GONE);
        } else if (type.equals(TYPE_ORDER_PICTURE)) {
            editText.setVisibility(View.GONE);
            orderPicture();
            finish();
        }
        if (mOnlyChoose) {
            editText.setVisibility(View.GONE);
        }
    }

    /**
     * 判断输入框内容是否为空
     */
    private boolean checkEditText(EditText editText) {

        boolean isEmpty = false;

        if (TextUtils.isEmpty(editText.getText().toString())) {
            isEmpty = true;
            Toast.makeText(this, getString(R.string.input_empty),
                    Toast.LENGTH_SHORT).show();
        } else {
            isEmpty = false;
        }

        return isEmpty;
    }

    @Override
    protected void onResume() {
        List<ImageInfo> infos = MyApplication.getInstance().imageInfos;
        if (infos != null)
            gridView.setAdapter(new ChooseGridAdapter(this, infos));
        super.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 2000:
                if (resultCode == RESULT_OK) {
                    String photoPath = WebViewUtil.getPhotoPath();
                    if (!TextUtils.isEmpty(photoPath)) {
                        ImageInfo imageInfo = new ImageInfo();
                        imageInfo.path = photoPath;
                        if (MyApplication.getInstance().getImageInfosSize() >= 8) {
                            Toast.makeText(ChooseImageAvtivity.this, "最多只能选八张",
                                    Toast.LENGTH_LONG).show();
                        } else {
                            MyApplication.getInstance().addImageInfo(imageInfo);
                        }

                        if (!WebViewUtil.isHaveChoose) {
                            Intent intent = new Intent(ChooseImageAvtivity.this,
                                    ChooseImageAvtivity.class);

                            startActivity(intent);
                        }
                    }
                }
                break;

            default:
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 上传回复信息
     */

    private void repalyMessage() {
        String url = XLGConstants.URL + "Ok-comment";

        HttpLoader<ReplayBean> httpLoader = new HttpLoader<ReplayBean>(this,
                url, ReplayBean.class);
        AjaxParams params = new AjaxParams();
        try {
            List<ImageInfo> imageInfos = MyApplication.getInstance().imageInfos;
            for (int i = 0; i < imageInfos.size(); i++) {
                params.put("file" + i, new File(imageInfos.get(i).path));
            }
            if (WebViewUtil.replayInfo != null) {
                params.put("comment_name", editText.getText().toString());
                params.put("topic_id", WebViewUtil.replayInfo.getTopic_id());
            }
            params.put(
                    "id",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_ID, ""));
            params.put(
                    "user_key",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_KEY, ""));
            params.put(
                    "token",
                    XLGDataUtils.encrypt2MD5("Ok" + XLGConstants.SECRET
                            + "comment"));
            params.put("device_type", "1");
            httpLoader
                    .setReplayManagerListener(new OnWebLoadListenerReplay<ReplayBean>() {

                        @Override
                        public void OnStart() {
                            showDialog("发表中");
                        }

                        @Override
                        public void OnError(String error) {
                            dismissDialog();
                            Toast.makeText(ChooseImageAvtivity.this, "发表失败",
                                    Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onSuccess(ReplayBean t, String result) {
                            dismissDialog();
                            if (t != null) {
                                if (t.code.equals("000000")) {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            "发表成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(
                                            XLGConstants.REPLAY_SUCCESS);
                                    intent.putExtra("content", result);
                                    intent.putExtra("type", type);
                                    sendBroadcast(intent);
                                    finish();
                                } else {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            t.message, Toast.LENGTH_SHORT)
                                            .show();
                                }
                            }
                        }
                    });
            httpLoader.loadReplayData(params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 上传主题信息
     */

    private void topicMessage() {
        String url = XLGConstants.URL + "Ok-addtopic";

        HttpLoader<ReplayBean> httpLoader = new HttpLoader<ReplayBean>(this,
                url, ReplayBean.class);
        AjaxParams params = new AjaxParams();
        try {
            List<ImageInfo> imageInfos = MyApplication.getInstance().imageInfos;
            for (int i = 0; i < imageInfos.size(); i++) {
                params.put("file" + i, new File(imageInfos.get(i).path));
            }
            if (WebViewUtil.topicInfo != null) {
                params.put("topic_name", editText.getText().toString());
                params.put("party_id", WebViewUtil.topicInfo.getParty_id());
            }
            params.put(
                    "id",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_ID, ""));
            params.put(
                    "user_key",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_KEY, ""));
            params.put(
                    "token",
                    XLGDataUtils.encrypt2MD5("Ok" + XLGConstants.SECRET
                            + "addtopic"));
            params.put("device_type", "1");
            httpLoader
                    .setReplayManagerListener(new OnWebLoadListenerReplay<ReplayBean>() {

                        @Override
                        public void OnStart() {
                            showDialog("发表中");

                        }

                        @Override
                        public void OnError(String error) {
                            dismissDialog();
                            Toast.makeText(ChooseImageAvtivity.this, "发表失败",
                                    Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onSuccess(ReplayBean t, String result) {
                            dismissDialog();
                            if (t != null) {
                                if (t.code.equals("000000")) {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            "发表成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(
                                            XLGConstants.REPLAY_SUCCESS);
                                    intent.putExtra("content", result);
                                    intent.putExtra("type", type);
                                    sendBroadcast(intent);
                                    finish();
                                } else {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            t.message, Toast.LENGTH_SHORT)
                                            .show();
                                }
                            }
                        }
                    });
            httpLoader.loadReplayData(params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 上传场景信息
     */

    private void musicScene() {
        String url = XLGConstants.URL + "Ok-addscene";
        HttpLoader<MusicSceneBean> httpLoader = new HttpLoader<MusicSceneBean>(
                this, url, MusicSceneBean.class);
        AjaxParams params = new AjaxParams();
        try {
            List<ImageInfo> imageInfos = MyApplication.getInstance().imageInfos;
            for (int i = 0; i < imageInfos.size(); i++) {
                params.put("file" + i, new File(imageInfos.get(i).path));
            }
            params.put("title", editText.getText().toString());
            params.put(
                    "id",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_ID, ""));
            params.put(
                    "user_key",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_KEY, ""));
            params.put(
                    "token",
                    XLGDataUtils.encrypt2MD5("Ok" + XLGConstants.SECRET
                            + "addscene"));
            params.put("device_type", "1");
            httpLoader
                    .setManagerListener(new OnWebLoadListener<MusicSceneBean>() {

                        @Override
                        public void OnStart() {
                            showDialog("上传中");

                        }

                        @Override
                        public void OnError(String error) {
                            dismissDialog();
                            Toast.makeText(ChooseImageAvtivity.this, "自定义失败",
                                    Toast.LENGTH_SHORT).show();

                        }

                        @Override
                        public void onSuccess(MusicSceneBean t) {
                            dismissDialog();
                            if (t != null) {
                                if (t.getCode().equals("000000")) {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            "自定义成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(
                                            XLGConstants.REPLAY_SUCCESS);
                                    intent.putExtra("content", t.getResult());
                                    intent.putExtra("type", type);
                                    sendBroadcast(intent);
                                    finish();
                                } else {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            t.getMessage(), Toast.LENGTH_SHORT)
                                            .show();
                                }
                            }

                        }
                    });

            httpLoader.loadData(params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 修改用户头像
     */

    private void modifyPhoto() {
        String url = XLGConstants.URL + "Login-memberhead";
        HttpLoader<ModifyPhotoBean> httpLoader = new HttpLoader<ModifyPhotoBean>(
                this, url, ModifyPhotoBean.class);
        AjaxParams params = new AjaxParams();
        try {
            List<ImageInfo> imageInfos = MyApplication.getInstance().imageInfos;
            for (int i = 0; i < imageInfos.size(); i++) {
                params.put("file" + i, new File(imageInfos.get(i).path));

                EventBus.getDefault().post(
                        new FirstEvent(new File(imageInfos.get(i).path)));

                System.out
                        .println("图片的地址等于" + new File(imageInfos.get(i).path));
            }
            params.put(
                    "id",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_ID, ""));
            params.put(
                    "user_key",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_KEY, ""));
            params.put(
                    "token",
                    XLGDataUtils.encrypt2MD5("Login" + XLGConstants.SECRET
                            + "memberhead"));
            params.put("device_type", "1");
            httpLoader
                    .setReplayManagerListener(new OnWebLoadListenerReplay<ModifyPhotoBean>() {

                        @Override
                        public void OnStart() {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void OnError(String error) {
                            Toast.makeText(ChooseImageAvtivity.this, "修改失败",
                                    Toast.LENGTH_SHORT).show();

                        }

                        @Override
                        public void onSuccess(ModifyPhotoBean t, String result) {
                            if (t != null) {
                                if (t.getCode().equals("000000")) {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            "修改成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(
                                            XLGConstants.REPLAY_SUCCESS);
                                    intent.putExtra("content", result);
                                    intent.putExtra("type", type);
                                    sendBroadcast(intent);

                                    finish();
                                } else {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            t.getMessage(), Toast.LENGTH_SHORT)
                                            .show();
                                }
                            }

                        }
                    });

            httpLoader.loadReplayData(params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 发货订单图片
     */

    private void orderPicture() {
        String url = XLGConstants.URL + "Ok-orderdelivery";
        HttpLoader<ResultBean> httpLoader = new HttpLoader<ResultBean>(this,
                url, ResultBean.class);
        AjaxParams params = new AjaxParams();
        try {
            List<ImageInfo> imageInfos = MyApplication.getInstance().imageInfos;
            for (int i = 0; i < imageInfos.size(); i++) {
                params.put("file" + i, new File(imageInfos.get(i).path));
            }
            if (WebViewUtil.pictureOrderInfo != null) {
                params.put("ordersid",
                        WebViewUtil.pictureOrderInfo.getOrdersid());
                // params.put("limitcount",
                // WebViewUtil.pictureOrderInfo.limitcount);
            }
            params.put(
                    "id",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_ID, ""));
            params.put(
                    "user_key",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_KEY, ""));
            params.put(
                    "token",
                    XLGDataUtils.encrypt2MD5("Ok" + XLGConstants.SECRET
                            + "orderdelivery"));
            params.put("device_type", "1");


            httpLoader
                    .setReplayManagerListener(new OnWebLoadListenerReplay<ResultBean>() {

                        @Override
                        public void OnStart() {
                            showDialog("上传中");
                        }

                        @Override
                        public void OnError(String error) {
                            dismissDialog();
                            Toast.makeText(ChooseImageAvtivity.this, "上传失败",
                                    Toast.LENGTH_SHORT).show();

                        }

                        @Override
                        public void onSuccess(ResultBean t, String result) {
                            dismissDialog();
                            if (t != null) {
                                if (t.getCode().equals("000000")) {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            "上传成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(
                                            XLGConstants.REPLAY_SUCCESS);
                                    intent.putExtra("content", result);
                                    intent.putExtra("type", type);
                                    sendBroadcast(intent);
                                    finish();
                                } else {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            t.getMessage(), Toast.LENGTH_SHORT)
                                            .show();
                                }
                            }

                        }
                    });

            httpLoader.loadReplayData(params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 评价订单
     */

    private void commentOrder() {
        String url = XLGConstants.URL + "Login-evalu";
        HttpLoader<ResultBean> httpLoader = new HttpLoader<ResultBean>(this,
                url, ResultBean.class);
        AjaxParams params = new AjaxParams();
        try {
            List<ImageInfo> imageInfos = MyApplication.getInstance().imageInfos;
            for (int i = 0; i < imageInfos.size(); i++) {
                params.put("file" + i, new File(imageInfos.get(i).path));
            }
            if (WebViewUtil.commentOrderInfo != null) {
                params.put("oid", WebViewUtil.commentOrderInfo.getOid());
                params.put("pid", WebViewUtil.commentOrderInfo.getPid());
            }
            params.put(
                    "id",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_ID, ""));
            params.put(
                    "user_key",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_KEY, ""));
            params.put(
                    "token",
                    XLGDataUtils.encrypt2MD5("Login" + XLGConstants.SECRET
                            + "evalu"));
            params.put("device_type", "1");
            httpLoader
                    .setReplayManagerListener(new OnWebLoadListenerReplay<ResultBean>() {

                        @Override
                        public void OnStart() {
                            showDialog("上传中");
                        }

                        @Override
                        public void OnError(String error) {
                            dismissDialog();
                            Toast.makeText(ChooseImageAvtivity.this, "上传失败",
                                    Toast.LENGTH_SHORT).show();

                        }

                        @Override
                        public void onSuccess(ResultBean t, String result) {
                            dismissDialog();
                            if (t != null) {
                                if (t.getCode().equals("000000")) {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            "上传成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(
                                            XLGConstants.REPLAY_SUCCESS);
                                    intent.putExtra("content", result);
                                    intent.putExtra("type", type);
                                    sendBroadcast(intent);
                                    finish();
                                } else {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            t.getMessage(), Toast.LENGTH_SHORT)
                                            .show();
                                }
                            }

                        }
                    });

            httpLoader.loadReplayData(params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 意见反馈
     */

    private void suggestMessage() {
        String url = XLGConstants.URL + "Login-yijian";
        HttpLoader<ResultBean> httpLoader = new HttpLoader<ResultBean>(this,
                url, ResultBean.class);
        AjaxParams params = new AjaxParams();
        try {
            List<ImageInfo> imageInfos = MyApplication.getInstance().imageInfos;
            for (int i = 0; imageInfos != null && i < imageInfos.size(); i++) {
                File saveMyBitmap = MyBitmapFactory.saveMyBitmap(
                        "compressImage" + i + ".jpg",
                        MyBitmapFactory.getMatrixImage(imageInfos.get(i).path));
                if (saveMyBitmap == null) {
                    continue;
                }
                params.put("file" + i, saveMyBitmap);
            }
            params.put(
                    "id",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_ID, ""));
            params.put(
                    "user_key",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_KEY, ""));
            params.put(
                    "token",
                    XLGDataUtils.encrypt2MD5("Login" + XLGConstants.SECRET
                            + "yijian"));
            params.put("device_type", "1");
            httpLoader
                    .setReplayManagerListener(new OnWebLoadListenerReplay<ResultBean>() {

                        @Override
                        public void OnStart() {
                            showDialog("上传中");
                        }

                        @Override
                        public void OnError(String error) {
                            dismissDialog();
                            Toast.makeText(ChooseImageAvtivity.this, "上传失败",
                                    Toast.LENGTH_SHORT).show();

                        }

                        @Override
                        public void onSuccess(ResultBean t, String result) {
                            dismissDialog();
                            if (t != null) {
                                if (t.getCode().equals("000000")) {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            "上传成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(
                                            XLGConstants.REPLAY_SUCCESS);
                                    intent.putExtra("content", result);
                                    intent.putExtra("type", type);
                                    sendBroadcast(intent);
                                    finish();
                                } else {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            t.getMessage(), Toast.LENGTH_SHORT)
                                            .show();
                                }
                            }

                        }
                    });

            httpLoader.loadReplayData(params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 退款留言
     */

    private void sellMessage() {
        String url = XLGConstants.URL + "Login-aftersales";
        HttpLoader<ResultBean> httpLoader = new HttpLoader<ResultBean>(this,
                url, ResultBean.class);
        AjaxParams params = new AjaxParams();
        try {
            List<ImageInfo> imageInfos = MyApplication.getInstance().imageInfos;
            for (int i = 0; i < imageInfos.size(); i++) {
                params.put("file" + i, new File(imageInfos.get(i).path));
            }
            params.put(
                    "id",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_ID, ""));
            params.put(
                    "user_key",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_KEY, ""));
            params.put(
                    "token",
                    XLGDataUtils.encrypt2MD5("Login" + XLGConstants.SECRET
                            + "aftersales"));
            params.put("device_type", "1");
            httpLoader
                    .setReplayManagerListener(new OnWebLoadListenerReplay<ResultBean>() {

                        @Override
                        public void OnStart() {
                            showDialog("上传中");
                        }

                        @Override
                        public void OnError(String error) {
                            dismissDialog();
                            Toast.makeText(ChooseImageAvtivity.this, "上传失败",
                                    Toast.LENGTH_SHORT).show();

                        }

                        @Override
                        public void onSuccess(ResultBean t, String result) {
                            dismissDialog();
                            if (t != null) {
                                if (t.getCode().equals("000000")) {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            "上传成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(
                                            XLGConstants.REPLAY_SUCCESS);
                                    intent.putExtra("content", result);
                                    intent.putExtra("type", type);
                                    sendBroadcast(intent);
                                    finish();
                                } else {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            t.getMessage(), Toast.LENGTH_SHORT)
                                            .show();
                                }
                            }

                        }
                    });

            httpLoader.loadReplayData(params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 通用的图片上传
     */

    private void customerUpload() {
        String url = XLGConstants.URL + "Ok-comupload";
        HttpLoader<ResultBean> httpLoader = new HttpLoader<ResultBean>(this,
                url, ResultBean.class);
        AjaxParams params = new AjaxParams();
        try {
            List<ImageInfo> imageInfos = MyApplication.getInstance().imageInfos;
            for (int i = 0; i < imageInfos.size(); i++) {
                params.put("file" + i, new File(imageInfos.get(i).path));
            }
            params.put(
                    "id",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_ID, ""));
            params.put(
                    "user_key",
                    PreferenceUtils.getInstance().getSettingStr(
                            PreferencesConfig.USER_KEY, ""));
            params.put(
                    "token",
                    XLGDataUtils.encrypt2MD5("Ok" + XLGConstants.SECRET
                            + "comupload"));
            params.put("device_type", "1");
            httpLoader
                    .setReplayManagerListener(new OnWebLoadListenerReplay<ResultBean>() {

                        @Override
                        public void OnStart() {
                            showDialog("上传中");
                        }

                        @Override
                        public void OnError(String error) {
                            dismissDialog();
                            Toast.makeText(ChooseImageAvtivity.this, "上传失败",
                                    Toast.LENGTH_SHORT).show();

                        }

                        @Override
                        public void onSuccess(ResultBean t, String result) {
                            dismissDialog();
                            if (t != null) {
                                if (t.getCode().equals("000000")) {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            "上传成功", Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(
                                            XLGConstants.REPLAY_SUCCESS);
                                    intent.putExtra("content", result);
                                    intent.putExtra("type", type);
                                    sendBroadcast(intent);
                                    finish();
                                } else {
                                    Toast.makeText(ChooseImageAvtivity.this,
                                            t.getMessage(), Toast.LENGTH_SHORT)
                                            .show();
                                }
                            }

                        }
                    });

            httpLoader.loadReplayData(params);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        MyApplication.getInstance().deleteAllImageInfos();
        WebViewUtil.isHaveChoose = false;
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sure:
                if (mOnlyChoose) {
                    ChooseGridAdapter adapter = (ChooseGridAdapter) gridView.getAdapter();
                    ArrayList<String> images = new ArrayList<>();
                    for (int i = 0; i < adapter.getCount(); i++) {
                        if (adapter.getItem(i) != null) {
                            images.add(((ImageInfo) adapter.getItem(i)).path);
                        }
                    }
                    Intent intent = new Intent();
                    intent.putStringArrayListExtra("images", images);
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    if (type.equals(TYPE_REPLAY)) {
                        if (checkEditText(editText)) {
                            return;
                        }
                        repalyMessage();
                    } else if (type.equals(TYPE_TOPIC)) {
                        if (checkEditText(editText)) {
                            return;
                        }
                        topicMessage();
                    } else if (type.equals(TYPE_MUSIC)) {
                        if (checkEditText(editText)) {
                            return;
                        }
                        musicScene();
                    } else if (type.equals(TYPE_COMMENT_ORDER)) {
                        commentOrder();
                    } else if (type.equals(TYPE_SELL_MESSAGE)) {
                        sellMessage();
                    } else if (type.equals(TYPE_SUGGEST_MESSAGE)) {
                        suggestMessage();
                    } else if (type.equals(TYPE_CUSTOMER_UPLOAD)) {
                        customerUpload();
                    }
                }
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }

    }
}
